using System;
using System.Collections;
using Jayrock.Json;
using Jayrock.Json.Conversion.Converters;

namespace NCouch.Json
{
	sealed class ObjectReferenceTracker
	{
		// Fields
		readonly ArrayList _stack = new ArrayList(6);

		// Methods
		public void Pop(object value)
		{
			int index = _stack.Count - 1;
			_stack.RemoveAt(index);
		}

		public void PushNew(object value)
		{
			for (int i = _stack.Count - 1; i >= 0; i--)
			{
				if (ReferenceEquals(_stack[i], value))
				{
					throw new JsonException(
						String.Format(
							"{0} does not support export of an object graph containing circular references. A value of type {1} has already been exported.",
							typeof (ComponentExporter).FullName, value.GetType().FullName));
				}
			}
			_stack.Add(value);
		}
	}
}